package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.User;
import com.example.springboot.service.IBorrowService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Borrow;

import org.springframework.web.bind.annotation.RestController;

/**
* BorrowAPI接口
*/
@RestController
@RequestMapping("/borrow")
public class BorrowController {

    @Autowired
    private IBorrowService borrowService;

    /**
     * 新增
     */
    @PostMapping
    public Result save(@RequestBody Borrow borrow) {
        borrowService.save(borrow);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping
    public Result update(@RequestBody Borrow borrow) {
        borrowService.updateById(borrow);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        borrowService.removeById(id);
        return Result.success();
    }

    /**
     * 查询全部数据
     */
    @GetMapping
    public Result findAll() {
        return Result.success(borrowService.list());
    }

    /**
     * 查询详情
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(borrowService.getById(id));
    }

    /**
     * 分页查询数据
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Borrow> queryWrapper = new LambdaQueryWrapper<Borrow>().orderByDesc(Borrow::getId);
        queryWrapper.like(StrUtil.isNotBlank(name), Borrow::getName, name);
        User currentUser = TokenUtils.getCurrentUser();
        if (currentUser.getRole().equals("USER")){
            queryWrapper.eq(Borrow::getUserId,currentUser.getId());
        }
        Page<Borrow> page = borrowService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    @GetMapping("/page/front")
    public Result findPageFront(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        LambdaQueryWrapper<Borrow> queryWrapper = new LambdaQueryWrapper<Borrow>().orderByDesc(Borrow::getId);
        queryWrapper.like(StrUtil.isNotBlank(name), Borrow::getName, name);
        User currentUser = TokenUtils.getCurrentUser();
        if (currentUser.getRole().equals("USER")){
            queryWrapper.eq(Borrow::getUserId,currentUser.getId());
        }
        Page<Borrow> page = borrowService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 获取借书数量：所有的、借阅中的、已归还的
     */
    @GetMapping("/borrowCount")
    public Result borrowCount() {
        return Result.success(borrowService.borrowCount());
    }

    @GetMapping("/borrowEcharts")
    public Result borrowEcharts() {
        return Result.success(borrowService.borrowEcharts());
    }


}
